package com.songhuan.web.service.impl;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.songhuan.web.dto.*;
import com.songhuan.web.dto.query.*;
import com.songhuan.web.entity.*;
import com.songhuan.web.mapper.*;
import com.songhuan.web.service.*;
import com.songhuan.web.tools.dto.*;
import com.songhuan.web.dto.AppUserDto;
import com.songhuan.web.dto.OrganizationDto;
import com.songhuan.web.dto.query.OrganizationPagedInput;
import com.songhuan.web.entity.AppUser;
import com.songhuan.web.entity.Organization;
import com.songhuan.web.mapper.AppUserMapper;
import com.songhuan.web.mapper.OrganizationMapper;
import com.songhuan.web.service.OrganizationService;
import com.songhuan.web.tools.Extension;
import com.songhuan.web.tools.dto.PagedResult;
import org.apache.commons.beanutils.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import lombok.SneakyThrows;
import com.songhuan.web.tools.*;
/**
 * 班级功能实现类
 */
@Service
public class OrganizationServiceImpl extends ServiceImpl<OrganizationMapper, Organization> implements OrganizationService {

	 /**
     * 操作数据库AppUser表mapper对象
     */
    @Autowired
    private AppUserMapper _AppUserMapper;
    /**
     * 操作数据库的Organization表mapper对象
     */
    @Autowired
    private OrganizationMapper _OrganizationMpper;


    /**
     * 班级分页查询
     */
    @SneakyThrows
    @Override
    public PagedResult<OrganizationDto> List(OrganizationPagedInput input) {

      
        //声明一个支持班级查询的(拉姆达)表达式
        LambdaQueryWrapper<Organization> queryWrapper = Wrappers.<Organization>lambdaQuery()
                .eq(input.getId()!=null&&input.getId()!=0,Organization::getId,input.getId())
                .eq(input.getCreatorId()!=null,Organization::getCreatorId,input.getCreatorId());
   //如果前端搜索传入查询条件则拼接查询条件

        if(Extension.isNotNullOrEmpty(input.getName())) {
            queryWrapper=queryWrapper.like(Organization::getName,input.getName());
       	 }

        if(Extension.isNotNullOrEmpty(input.getAddress())) {
            queryWrapper=queryWrapper.eq(Organization::getAddress,input.getAddress());
       	 }
         if(input.getLongitudeStartRange()!=null)
          {
            queryWrapper=queryWrapper.gt(Organization::getLongitude,input.getLongitudeStartRange());
          }
      		 if(input.getLongitudeEndRange()!=null)
          {
            queryWrapper=queryWrapper.lt(Organization::getLongitude,input.getLongitudeEndRange());
          }
         if(input.getLatitudeStartRange()!=null)
          {
            queryWrapper=queryWrapper.gt(Organization::getLatitude,input.getLatitudeStartRange());
          }
      		 if(input.getLatitudeEndRange()!=null)
          {
            queryWrapper=queryWrapper.lt(Organization::getLatitude,input.getLatitudeEndRange());
          }
         if(input.getDistanceStartRange()!=null)
          {
            queryWrapper=queryWrapper.gt(Organization::getDistance,input.getDistanceStartRange());
          }
      		 if(input.getDistanceEndRange()!=null)
          {
            queryWrapper=queryWrapper.lt(Organization::getDistance,input.getDistanceEndRange());
          }

        if(Extension.isNotNullOrEmpty(input.getLinkName())) {
            queryWrapper=queryWrapper.eq(Organization::getLinkName,input.getLinkName());
       	 }

        if(Extension.isNotNullOrEmpty(input.getPhone())) {
            queryWrapper=queryWrapper.eq(Organization::getPhone,input.getPhone());
       	 }
      
        //按创建时间从大到小排序 最新的显示在最前面
        queryWrapper=queryWrapper.orderByDesc(Organization::getCreationTime);
        //构建一个分页查询的model
        Page<Organization> page = new Page<>(input.getPage(), input.getLimit());
          
   
         //从数据库进行分页查询获取班级数据
        IPage<Organization> pageRecords= _OrganizationMpper.selectPage(page, queryWrapper);

        //获取所有满足条件的数据行数
        Long totalCount= _OrganizationMpper.selectCount(queryWrapper);

        //把Organization实体转换成Organization传输模型
        List<OrganizationDto> items= Extension.copyBeanList(pageRecords.getRecords(),OrganizationDto.class);
        for (OrganizationDto item : items) {
          
          //查询出关联的创建用户信息
            AppUserDto CreatorAppUserDTO=new AppUserDto();
            AppUser CreatorAppUserEntity= _AppUserMapper.selectOne(Wrappers.<AppUser>lambdaQuery().eq(AppUser::getId,item.getCreatorId()));
           if(CreatorAppUserEntity!=null) {
                BeanUtils.copyProperties(CreatorAppUserDTO, CreatorAppUserEntity);
                item.setCreatorAppUserDto(CreatorAppUserDTO);
            }  
         
        }
      
        //返回一个分页结构给前端
        return PagedResult.GetInstance(items,totalCount);

    }
  
    /**
     * 单个班级查询
     */
    @SneakyThrows
    @Override
    public OrganizationDto Get(OrganizationPagedInput input) {
       if(input.getId()==null)
        {
            return new OrganizationDto();
        }
        PagedResult<OrganizationDto>  pagedResult =List(input);
        return pagedResult.getTotalCount()>0?pagedResult.getItems().stream().findFirst().get():new OrganizationDto();
    }

    /**
     *班级创建或者修改
     */
    @SneakyThrows
    @Override
    public OrganizationDto CreateOrEdit(OrganizationDto input) {

        //声明一个班级实体
        Organization Organization = new Organization();

        //把前端传入的input参数拷贝到班级实体
        BeanUtils.copyProperties(Organization,input);

        //调用数据库的增加或者修改方法
        saveOrUpdate(Organization);

        //定义一个返回给前端的班级传输模型
        OrganizationDto OrganizationDto=new OrganizationDto();

        //同理把操作的班级实体拷贝给班级传输模型
        BeanUtils.copyProperties(OrganizationDto,Organization);

        //把传输模型返回给前端
        return OrganizationDto;
    }



}
